everyauth = require('everyauth')

everyauth.google
  .myHostname('http://tucao.in')
  .appId('tucao.in')
  .appSecret('0BcCVOnnHF_we9qgiiEbYTfs')
  .scope('https://www.google.com/m8/feeds')
  .handleAuthCallbackError((req, res)->
    console.log arguments
    #If a user denies your app, Google will redirect the user to
    #/auth/facebook/callback?error=access_denied
    #This configurable route handler defines how you want to respond to
    #that.
    #If you do not configure this, everyauth renders a default fallback
    #view notifying the user that their authentication failed and why.
  )
  .findOrCreateUser((session, accessToken, accessTokenExtra, googleUserMetadata)->
    console.log arguments
    #find or create user logic goes here
    #Return a user or Promise that promises a user
    #Promises are created via
    #    var promise = this.Promise();
  )
  .redirectPath('/')
  .entryPath('/login/google/')
  .callbackPath('/login/google/callback/')

everyauth.github
  .appId('a5f9c7a01ab79c55ce1c')
  .appSecret('0b52d9009f2fec0d2a3e3cf4fc89a58dc7c827aa')
  .findOrCreateUser (session, accessToken, accessTokenExtra, githubUserMetadata)->
    console.log arguments
  .redirectPath('/')
  .entryPath('/login/github/')
  .callbackPath('/login/github/callback/')

weibo = require('weibo')

sina = module.exports = (app)->
  app.use everyauth.middleware()
  everyauth.helpExpress app

  weibo.init('tsina', '1158479878', '0f5ae4c24b80a4c22a0c62f15e12ab77', 'http://localhost:7000/login/sina/callback/')
  app.use weibo.oauth_middleware (oauth_user, referer, req, res, callback)->
    console.log oauth_user
    callback(true)
  , {
    home_url: 'http://localhost:7000'
    login_path: '/login/sina/'
    logout_path: '/logout/'
    blogtype: 'tsina'
    callback_path: '/login/sina/callback/'
    error_callback: (error, referer, user, req, res, next) ->
      console.log error, user
      next()
    get_access_token: (user, callback, ct) ->
      console.log user, ct
      callback(true)
  }

  #app.get "/login/sina/", (req, res)->
  #    res.write(arguments)
  #    res.end("aaaa")

  app.get "/login/sina/back/", (req, res)->
    console.log req.params

  app.get "/login/google/callback/", (req, res)->
    console.log arguments

  app.get "/login/github/callback/", (req, res)->
    console.log arguments

